package com.city.msmservice.controller;

import com.city.commonutils.Result;
import com.city.msmservice.service.MsnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * Created with IntelliJ IDEA.
 * User: Cheng
 * Date: 2020/9/26
 * Time: 17:24
 * Description: No Description
 */
@RestController
@RequestMapping("/edumsm/msm")
@CrossOrigin
public class MsnController {

    //通过redis对验证码进行设置有效时间
    @Autowired
    private RedisTemplate<String,String> redisTemplate;//springboot整和redis的工具类

    @Autowired
    private MsnService msnService;

    @GetMapping("send/{phone}")
    public Result send(@PathVariable String phone){
        //判断redis中验证码是否存在
        //存在，返回ok
        String o = redisTemplate.opsForValue().get(phone);
        if (!StringUtils.isEmpty(o)){
            return Result.ok();
        }

        //不存在，发送
        Boolean code = msnService.getCode(phone);
        return code?Result.ok():Result.error().message("短信发送失败，请重试！");
    }
}
